package com.hiwan.unions.activity.main.fragment.homeFragment;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.hiwan.unions.R;
import com.hiwan.unions.activity.main.fragment.base.RootFragment;
import com.hiwan.unions.activity.main.homeInform.InformActivity;
import com.hiwan.unions.adapter.bean.HomeBottonBean;
import com.hiwan.unions.adapter.fragmentAdapters.HomeAdapter;
import com.hiwan.unions.application.ConstantsValues;
import com.hiwan.unions.application.request.CallServer;
import com.hiwan.unions.application.request.ConstsNet;
import com.hiwan.unions.application.request.HttpListener;
import com.hiwan.unions.util.JsonUtil;
import com.hiwan.unions.util.PerfectClickListener;
import com.hiwan.unions.view.MyListView;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by llg on 2017/6/7.
 */
public class AFragment extends RootFragment implements HttpListener<JSONObject> {
    private MyListView myListView;
    View view1;
    HomeAdapter adapter;
    private ArrayList<HomeBottonBean> cDates = new ArrayList<HomeBottonBean>();
    private JSONObject jsonObject;
    private int pageNumber = 1, pageSize = 10;
    private String city;

    @Override
    public int getLayout() {
        canRefresh = false;
        canLoadMore = false;
        return R.layout.fragment_a;
    }

    @Override
    protected void Interface() {
        super.Interface();
        myListView = findView(R.id.myListview);
        view1 = LayoutInflater.from(mActivity).inflate(R.layout.fragment_footer, null, false);
        view1.setOnClickListener(new PerfectClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                InformActivity.startPreview(getActivity(), 0);
            }
        });
        myListView.addFooterView(view1);
        adapter = new HomeAdapter(mActivity, cDates);
        myListView.setAdapter(adapter);
        city = userInfoC.getString(ConstantsValues.CITYNAME, "成都市");
    }


    @Override
    protected void initData() {
        getDate();
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
        } else {
            if (!city.equals(userInfoC.getString(ConstantsValues.CITYNAME, "成都市"))) {
                getDate();
            }
        }
    }

    public void getDate() {
        showLoading();
        Request<JSONObject> request = NoHttp.createJsonObjectRequest(ConstsNet.article_list, RequestMethod.POST);
        request.add("is_city", 0);
        request.add("folder", "politics_article");
        request.add("pageNumber", pageNumber);
        request.add("pageSize", pageSize);
        CallServer.getRequestInstance().add(getActivity(), 0, request, this, false);
    }


    @Override
    public void onSucceed(int what, Response<JSONObject> response) {
        jsonObject = response.get();
        jsonObject = JsonUtil.getJSONObject(jsonObject, "data");
        if (pageNumber == 1) {
            cDates.clear();
            adapter.notifyDataSetChanged();
        }
        JSONArray jsonArray = JsonUtil.getJSONArray(jsonObject, "list");
        if (jsonArray.length() == 0 && pageNumber == 1) {
            showeMPTY("该分类下暂无资讯");
        } else {
            showContentViews();
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = JsonUtil.getJSONObject(jsonArray, i);
                HomeBottonBean homeBottonBean = new HomeBottonBean();
                homeBottonBean.setDate(JsonUtil.getString(jsonObject, "publish_time"));
                homeBottonBean.setImage_url(JsonUtil.getString(jsonObject, "image_url"));
                homeBottonBean.setImage_net_url(JsonUtil.getString(jsonObject, "image_net_url"));
                homeBottonBean.setTitle(JsonUtil.getString(jsonObject, "title"));
                homeBottonBean.setUuid(JsonUtil.getString(jsonObject, "uuid"));
                homeBottonBean.setJump_url(JsonUtil.getString(jsonObject, "jump_url"));
                cDates.add(homeBottonBean);
            }
            adapter.notifyDataSetChanged();
        }

    }

    @Override
    public void onFailed(int what, int errCode, String networkMillis, boolean type) {
        if (type) {
            showError();
        }
    }


    private void showContentViews() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                showContentView();
            }
        }, 2000);
    }
}
